/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb.awl.completion;

import java.util.List;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.netbeans.editor.BaseDocument;
import org.netbeans.editor.SyntaxSupport;
import org.netbeans.editor.Utilities;
import org.netbeans.modules.xml.schema.completion.spi.CompletionContext;
import org.netbeans.modules.xml.text.syntax.XMLSyntaxSupport;
import org.netbeans.spi.editor.completion.CompletionResultSet;
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
import org.openide.filesystems.FileObject;

/**
 *
 * @author user
 */
public class AwlCompletionQuery extends AsyncCompletionQuery {

    private final FileObject primaryFile;
    private JTextComponent component;
    private AwlCompletionContext context;


    public AwlCompletionQuery(FileObject primaryFile, JTextComponent component) {
        this.primaryFile = primaryFile;
        this.component = component;
    }

    @Override
    protected void query(CompletionResultSet crs, Document doc, int caretOffset)
    {
        AbstractCompletionItem1 endTagResultItem = CompletionUtil.getEndTagCompletionItem(
            component, (BaseDocument) doc);
        System.out.println("querying");
        List<AbstractCompletionItem1> completionItems = null;

        if ( (CompletionUtil.canProvideCompletion(component, (BaseDocument) doc)) ) {
            completionItems = getCompletionItems(doc, caretOffset);
        }
        if (endTagResultItem != null) crs.addItem(endTagResultItem);
        if ((completionItems != null) && (completionItems.size() > 0)) {
            crs.addAllItems(completionItems);
        }
        crs.finish();
    }

    List<AbstractCompletionItem1> getCompletionItems(Document document, int caretOffset) {
        List<AbstractCompletionItem1> completionItems = null;

        SyntaxSupport syntaxSupport = Utilities.getSyntaxSupport(component);
         XMLSyntaxSupport support = (syntaxSupport instanceof XMLSyntaxSupport ?
            (XMLSyntaxSupport) syntaxSupport : null);
        context = new AwlCompletionContext(primaryFile, support, caretOffset);

        //Step 2: Accumulate all models and initialize the context
        if( !context.initContext() || !context.initModels() ) {
            return null;
        }

//        Step 3: Query
        switch ( context.getCompletionType() ) {
            case COMPLETION_TYPE_ELEMENT_VALUE:
//                completionItems = CompletionUtil.getElementValues(context);
//                if ((completionItems != null) && (completionItems.size() > 0)) {
//                    break;
//                }
                break;

            case COMPLETION_TYPE_ELEMENT:
                completionItems = CompletionUtil.getElements(context);
                break;

            case COMPLETION_TYPE_ATTRIBUTE:
//                completionItems = CompletionUtil.getAttributes(context);
                break;

            case COMPLETION_TYPE_ATTRIBUTE_VALUE:
//                completionItems = CompletionUtil.getAttributeValues(context);
                break;

            case COMPLETION_TYPE_ENTITY:
                break;

            case COMPLETION_TYPE_NOTATION:
                break;

            default:
                break;
        }

        return completionItems;
    }
}
